﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Adapter
{
    class Program
    {
        static void Main(string[] args)
        {
            Adapter adapter = new Adapter();
            //Adapter<A, B> adapter1 = new Adapter<A, B>();
            //Adapter<C, B> adapter2 = new Adapter<C, B>();

            B b = new B();
            b.b1 = "b1";
            b.b2 = "b2";

            A a = new A();
            Console.WriteLine("before: " + (string.IsNullOrEmpty(a.a1) ? "empty" : a.a1)
                + " " + (string.IsNullOrEmpty(a.a2) ? "empty" : a.a2));
            adapter.AdapterType(a, b, new Dictionary<string, string>() { { "a1", "b1" }, { "a2", "b2" } });
            //adapter1.AdapterType(a, b, new Dictionary<string, string>() { { "a1", "b1" }, { "a2", "b2" } });
            Console.WriteLine("after: " + a.a1 + " " + a.a2);

            Console.WriteLine();
            C c = new C();
            Console.WriteLine("before: " + (string.IsNullOrEmpty(c.c1) ? "empty" : c.c1)
                + " " + (string.IsNullOrEmpty(c.c2) ? "empty" : c.c2));
            adapter.AdapterType(c, b, new Dictionary<string, string>() { { "c1", "b1" }, { "c2", "b2" } });
            //adapter2.AdapterType(c, b, new Dictionary<string, string>() { { "c1", "b1" }, { "c2", "b2" } });
            Console.WriteLine("after: " + c.c1 + " " + c.c2);



            Console.Read();
        }
    }

    class A
    {
        public string a1 { get; set; }
        public string a2 { get; set; }
    }

    class B
    {
        public string b1 { get; set; }
        public string b2 { get; set; }
    }

    class C
    {
        public string c1 { get; set; }
        public string c2 { get; set; }
    }

    class Adapter
    {
        public void AdapterType<T1, T2>(T1 obj1, T2 obj2, Dictionary<string, string> map)
        {
            foreach (KeyValuePair<string, string> item in map)
            {
                var p1 = obj1.GetType().GetProperty(item.Key);
                var p2 = obj2.GetType().GetProperty(item.Value);

                p1.SetValue(obj1, Convert.ChangeType(p2.GetValue(obj2), p1.PropertyType));
            }

        }
    }

    //class Adapter
    //{
    //    public void AdapterType(object obj1, object obj2, Dictionary<string, string> map)
    //    {
    //        foreach (KeyValuePair<string, string> item in map)
    //        {
    //            var p1 = obj1.GetType().GetProperty(item.Key);
    //            var p2 = obj2.GetType().GetProperty(item.Value);

    //            p1.SetValue(obj1, Convert.ChangeType(p2.GetValue(obj2), p1.PropertyType));
    //        }

    //    }
    //}
}
